/**
 * @author 李楠君
 * 短信请假
 */
package com.safe.ocs.services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.safe.ocs.beans.InputSMS;
import com.safe.ocs.beans.OutputSMS;
import com.safe.ocs.beans.Vocation;
import com.safe.ocs.utils.HibernateUtil;

public class ForwardSMS extends TimerTask {
	public static final SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd ");

	public void run() {
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		// String findMsg =
		// "select id,rmsg,rtelNo from InputSMS where rmsg like 'QJ%' and flag='false'";
		String findMsg = "select t1.id ,t1.rmsg,t1.rtelNo,t2.person_Name ,t2.person_ID,t2.dept_ID from InputSMS t1,ST_Person t2 where rtelNo = remark and rmsg like 'QJ%' and flag='false'";
		String findLeader = "select name,phoneNo,dept_ID from Leaders";
		// String findLeader =
		// "select name,phoneNo from Leaders where dept_ID=?";
		try {
			List inputList = session.createQuery(findMsg).list();
			List leaderList = session.createQuery(findLeader).list();
			if (inputList.size() > 0 && leaderList.size() > 0) {
				for (int i = 0; i < inputList.size(); i++) {
					Object[] msgArray = (Object[]) inputList.get(i);
					int id = (Integer) msgArray[0]; // 获取当前消息主键
					System.out.println(id);
					String msgContent = ((String) msgArray[1]).trim();
					String rtelNo = ((String) msgArray[2]).trim();// 发短信人的电话号码
					// String rname = "张三";
					String rname = ((String) msgArray[3]).trim();// 发短信人的姓名
					// String person_No = (String)msgArray[4];
					// String person_No = (String) msgArray[4];// 打卡人员编号
					int dept_ID = (Integer) msgArray[5];// 部门识别号
					// int person_ID = Integer.parseInt(person_No);
					int person_ID = (Integer) msgArray[4];
					System.out.println("获得考勤人员编号：" + person_ID
							+ "=============");
					System.out.println("获得考勤人员姓名：" + rname + "=============");
					System.out.println("获得考勤人员号码 ：" + rtelNo + "=============");
					System.out.println("获得考勤人员所属部门：" + dept_ID
							+ "=============");
					InputSMS iptMsg = (InputSMS) session.load(InputSMS.class,
							id);
					iptMsg.setFlag(true);
					String SDate = "20" + msgContent.substring(3, 5) + "-"
							+ msgContent.substring(5, 7) + "-"
							+ msgContent.substring(7, 9) + " "
							+ msgContent.substring(9, 11) + ":00:00";
					String EDate = "20" + msgContent.substring(11, 13) + "-"
							+ msgContent.substring(13, 15) + "-"
							+ msgContent.substring(15, 17) + " "
							+ msgContent.substring(17, 19) + ":59:59";
					String reason = msgContent.substring(19,
							msgContent.length()).trim();
					System.out.println("日期长度：" + SDate.length());
					if (this.valid(SDate) != true || this.valid(EDate) != true) {// 判断所获得的日期是否是日期型格式

						OutputSMS optMsgs = new OutputSMS();//返回短信发送失败提示信息
						optMsgs.setTelNo(rtelNo);
						optMsgs.setMsg("短信格式错误,发送失败,请重新发送!");
						optMsgs.setSendedStaut(false);
						session.save(optMsgs);
						System.out.println("短信格式错误");
						break;
					} else {
						OutputSMS optMsgs = new OutputSMS();//返回短信发送成功提示信息
						optMsgs.setTelNo(rtelNo);
						optMsgs.setMsg("短信发送成功!");
						optMsgs.setSendedStaut(false);
						session.save(optMsgs);
						System.out.println("短信格式正确");
						Vocation vocation = new Vocation();
						SimpleDateFormat sdf = new SimpleDateFormat(
								"yyyy-MM-dd hh:mm:ss");
						String ss = SDate.substring(0, 10);
						System.out.println("截取的日期" + ss);
						Date date = new Date();// 取系统当前时间
						vocation.setPerson_ID(person_ID);
						vocation.setName(rname);
						vocation.setPhoneNo(rtelNo);
						vocation.setReason(reason);
						vocation.setVocateFrom(sdf.parse(SDate));
						vocation.setVocateTo(sdf.parse(EDate));
						vocation.setUpdate_Time(date);
						session.save(vocation);
						String findDepartment = "select dept_Chief,dept_No from ST_Department where Dept_ID=?";
						Query query = session.createQuery(findDepartment);
						query.setLong(0, dept_ID);
						List listDepartment = query.list();
						for (int a = 0; a < listDepartment.size(); a++) {
							Object[] departArray = (Object[]) listDepartment
									.get(a);
							String dept_Chief = (String) departArray[0];
							String dept_No = ((String) departArray[1]).trim();
							if (dept_Chief.equals(rname)) {// 判断该发短息人是否是领导
								String Ldept_No = dept_No.substring(0,
										dept_No.length() - 2).trim();// 其上级领导所属的部门编号
								System.out
										.println(Ldept_No + "---------------");
								String findLeaderNew = "select t1.name,t1.phoneNo,t1.dept_ID from Leaders t1 left join ST_Department t2 on t1.dept_ID=t2.dept_ID Where t2.dept_No="
										+ Ldept_No;
								Query querys = session
										.createSQLQuery(findLeaderNew);
								List listLeader = querys.list();
								for (int j = 0; j < listLeader.size(); j++) {
									Object[] leaderArray = (Object[]) listLeader
											.get(j);
									OutputSMS optMsg = new OutputSMS();
									String telNo = (String) leaderArray[1];
									int dept_id = (Integer) leaderArray[2];
									System.out.println("领导所属的部门：" + dept_id);
									if ("".equals(reason))
										reason = "无";
									optMsg.setMsg(rname + "请假，请假时间为：" + SDate
											+ "到" + EDate + "请假原因:" + reason);
									System.out.println(telNo);
									optMsg.setTelNo(telNo.trim());
									optMsg.setSendedStaut(false);
									session.save(optMsg);
								}

							} else {// 该人不是领导
								for (int j = 0; j < leaderList.size(); j++) {
									Object[] leaderArray = (Object[]) leaderList
											.get(j);
									OutputSMS optMsg = new OutputSMS();
									String telNo = (String) leaderArray[1];
									int dept_id = (Integer) leaderArray[2];
									if (dept_ID == dept_id) {
										if ("".equals(reason))
											reason = "无";
										System.out
												.println("领导所属的部门：" + dept_id);
										optMsg.setMsg(rname + "请假，请假时间为："
												+ SDate + "到" + EDate + "请假原因:"
												+ reason);
										System.out.println(telNo);
										optMsg.setTelNo(telNo.trim());
										optMsg.setSendedStaut(false);
										session.save(optMsg);
									}
								}
							}
						}

					}
				}
			}
			tr.commit();
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	public boolean valid(String str) { // 判断字符串是否是日期型
		try {
			formatter.parse(str);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}